{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b2701d33-f3ee-4530-bb78-fad38a5bb25c",
   "metadata": {},
   "source": [
    "# 第四节、数据选取"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f8d07e23-ed2f-49b2-a1d3-e685d0a13e78",
   "metadata": {},
   "source": [
    "## （1）字段数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "04964ca6-08ea-43cc-943e-bdbed1ea9e13",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7fbbc70f-74d3-4fd2-9ae6-fd8a74ec9838",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Tensorflow</th>\n",
       "      <th>Keras</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>135</td>\n",
       "      <td>90</td>\n",
       "      <td>145</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>46</td>\n",
       "      <td>15</td>\n",
       "      <td>69</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>64</td>\n",
       "      <td>44</td>\n",
       "      <td>148</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>135</td>\n",
       "      <td>113</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>83</td>\n",
       "      <td>19</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  Tensorflow  Keras\n",
       "0     135          90    145\n",
       "1      46          15     69\n",
       "2      64          44    148\n",
       "3       1         135    113\n",
       "4      83          19      9"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(\n",
    "    data=np.random.randint(0, 150, size=(150, 3)),  # 计算机科目考试\n",
    "    columns=['Python','Tensorflow','Keras']\n",
    ")\n",
    "\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "461a34c0-af88-4e44-8c79-3af1d99d56c6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      135\n",
       "1       46\n",
       "2       64\n",
       "3        1\n",
       "4       83\n",
       "      ... \n",
       "145    117\n",
       "146     91\n",
       "147     94\n",
       "148     45\n",
       "149     72\n",
       "Name: Python, Length: 150, dtype: int32"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取某一列，方式一\n",
    "df['Python']  # 返回Series对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "75dda3dd-71f8-4fbc-aa15-898a6323314a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      135\n",
       "1       46\n",
       "2       64\n",
       "3        1\n",
       "4       83\n",
       "      ... \n",
       "145    117\n",
       "146     91\n",
       "147     94\n",
       "148     45\n",
       "149     72\n",
       "Name: Python, Length: 150, dtype: int32"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取某一列，方式二\n",
    "df.Python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e1601667-a066-49b6-a601-5a2034879a7b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      135\n",
       "1       46\n",
       "2       64\n",
       "3        1\n",
       "4       83\n",
       "      ... \n",
       "145    117\n",
       "146     91\n",
       "147     94\n",
       "148     45\n",
       "149     72\n",
       "Name: Python, Length: 150, dtype: int32"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取某一列，方式三\n",
    "df.loc[:,'Python']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ce839db4-42ae-472b-9248-d51b52f60f09",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      135\n",
       "1       46\n",
       "2       64\n",
       "3        1\n",
       "4       83\n",
       "      ... \n",
       "145    117\n",
       "146     91\n",
       "147     94\n",
       "148     45\n",
       "149     72\n",
       "Name: Python, Length: 150, dtype: int32"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取某一列，方式四\n",
    "df.iloc[:, 0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "ef150c3d-8101-427f-829e-eb71f150e167",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>135</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>83</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>145</th>\n",
       "      <td>117</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>146</th>\n",
       "      <td>91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147</th>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>148</th>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149</th>\n",
       "      <td>72</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>150 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     Python\n",
       "0       135\n",
       "1        46\n",
       "2        64\n",
       "3         1\n",
       "4        83\n",
       "..      ...\n",
       "145     117\n",
       "146      91\n",
       "147      94\n",
       "148      45\n",
       "149      72\n",
       "\n",
       "[150 rows x 1 columns]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取某一列，返回DataFrame数据\n",
    "df[['Python']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "3dd71fa4-73b2-4bd1-aa2f-310943a5764e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Keras</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>135</td>\n",
       "      <td>145</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>46</td>\n",
       "      <td>69</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>64</td>\n",
       "      <td>148</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>113</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>83</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>145</th>\n",
       "      <td>117</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>146</th>\n",
       "      <td>91</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147</th>\n",
       "      <td>94</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>148</th>\n",
       "      <td>45</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149</th>\n",
       "      <td>72</td>\n",
       "      <td>145</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>150 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     Python  Keras\n",
       "0       135    145\n",
       "1        46     69\n",
       "2        64    148\n",
       "3         1    113\n",
       "4        83      9\n",
       "..      ...    ...\n",
       "145     117     66\n",
       "146      91     10\n",
       "147      94     12\n",
       "148      45      6\n",
       "149      72    145\n",
       "\n",
       "[150 rows x 2 columns]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取多列，花式索引\n",
    "df[['Python', 'Keras']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "b66f09f6-e792-4825-8118-8c4f8e00b524",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Keras</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>135</td>\n",
       "      <td>145</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>46</td>\n",
       "      <td>69</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>64</td>\n",
       "      <td>148</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>113</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>83</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>145</th>\n",
       "      <td>117</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>146</th>\n",
       "      <td>91</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147</th>\n",
       "      <td>94</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>148</th>\n",
       "      <td>45</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149</th>\n",
       "      <td>72</td>\n",
       "      <td>145</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>150 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     Python  Keras\n",
       "0       135    145\n",
       "1        46     69\n",
       "2        64    148\n",
       "3         1    113\n",
       "4        83      9\n",
       "..      ...    ...\n",
       "145     117     66\n",
       "146      91     10\n",
       "147      94     12\n",
       "148      45      6\n",
       "149      72    145\n",
       "\n",
       "[150 rows x 2 columns]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取多列，切片\n",
    "df.iloc[:,::2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "f52d8a1a-e63b-4a1c-87a3-6b634877a2de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Tensorflow</th>\n",
       "      <th>Keras</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>135</td>\n",
       "      <td>90</td>\n",
       "      <td>145</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  Tensorflow  Keras\n",
       "0     135          90    145"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取一行\n",
    "df.loc[[0]]   # 返回DataFrame\n",
    "# 注意：loc里面的0不是索引，而是行名，只是这里凑巧也是0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "f45a3fde-3d75-407b-a446-1b3ba7778b10",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Python        135\n",
       "Tensorflow     90\n",
       "Keras         145\n",
       "Name: 0, dtype: int32"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取一行\n",
    "df.loc[0]   # 返回Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "48ae0ce6-573f-4fc3-ad3f-29710ce46d54",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Python        135\n",
       "Tensorflow     90\n",
       "Keras         145\n",
       "Name: 0, dtype: int32"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取一行\n",
    "df.iloc[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "0b5e3466-7efe-412f-a1e4-fb4ba75a168f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Tensorflow</th>\n",
       "      <th>Keras</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>135</td>\n",
       "      <td>90</td>\n",
       "      <td>145</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  Tensorflow  Keras\n",
       "0     135          90    145"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取一行\n",
    "df.iloc[[0]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "aa16a154-c6fe-4c8b-b52e-22c165b1cdc3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Tensorflow</th>\n",
       "      <th>Keras</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>70</td>\n",
       "      <td>7</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>71</td>\n",
       "      <td>19</td>\n",
       "      <td>43</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>97</td>\n",
       "      <td>30</td>\n",
       "      <td>149</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>101</td>\n",
       "      <td>27</td>\n",
       "      <td>138</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>86</td>\n",
       "      <td>25</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>69</td>\n",
       "      <td>145</td>\n",
       "      <td>77</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    Python  Tensorflow  Keras\n",
       "5       70           7     17\n",
       "6       71          19     43\n",
       "7       97          30    149\n",
       "8      101          27    138\n",
       "9       86          25     19\n",
       "10      69         145     77"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取多行\n",
    "df.iloc[5: 11]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "f8ee58a6-bb00-401b-8259-c8d7648114cf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Tensorflow</th>\n",
       "      <th>Keras</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>135</td>\n",
       "      <td>90</td>\n",
       "      <td>145</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>83</td>\n",
       "      <td>19</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>86</td>\n",
       "      <td>25</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  Tensorflow  Keras\n",
       "0     135          90    145\n",
       "4      83          19      9\n",
       "9      86          25     19"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取多行\n",
    "df.iloc[[0, 4, 9]]   # 获取第1行、第5行、第8行"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a936a0e-4d0b-4572-954e-3c6d168fe6d3",
   "metadata": {},
   "source": [
    "## （2）标签选择"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "fae91831-0534-457c-b45b-559d10d8df82",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "1c41fd0d-dc5c-454e-995c-987195f97c76",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Tensorflow</th>\n",
       "      <th>Keras</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>131</td>\n",
       "      <td>43</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>11</td>\n",
       "      <td>102</td>\n",
       "      <td>140</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>15</td>\n",
       "      <td>113</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>90</td>\n",
       "      <td>11</td>\n",
       "      <td>118</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>55</td>\n",
       "      <td>77</td>\n",
       "      <td>119</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>F</th>\n",
       "      <td>130</td>\n",
       "      <td>8</td>\n",
       "      <td>115</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>G</th>\n",
       "      <td>100</td>\n",
       "      <td>135</td>\n",
       "      <td>87</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>H</th>\n",
       "      <td>26</td>\n",
       "      <td>1</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I</th>\n",
       "      <td>45</td>\n",
       "      <td>36</td>\n",
       "      <td>83</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>J</th>\n",
       "      <td>94</td>\n",
       "      <td>51</td>\n",
       "      <td>105</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  Tensorflow  Keras\n",
       "A     131          43      9\n",
       "B      11         102    140\n",
       "C      15         113      2\n",
       "D      90          11    118\n",
       "E      55          77    119\n",
       "F     130           8    115\n",
       "G     100         135     87\n",
       "H      26           1     15\n",
       "I      45          36     83\n",
       "J      94          51    105"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = pd.DataFrame(\n",
    "    data=np.random.randint(0, 150, size=(10, 3)),   # 计算机科目考试成绩\n",
    "    index=list('ABCDEFGHIJ'),\n",
    "    columns=['Python','Tensorflow','Keras']\n",
    ")\n",
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "edd466d6-e725-4d4f-848d-b1a4d927c879",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Tensorflow</th>\n",
       "      <th>Keras</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>131</td>\n",
       "      <td>43</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>15</td>\n",
       "      <td>113</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>90</td>\n",
       "      <td>11</td>\n",
       "      <td>118</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  Tensorflow  Keras\n",
       "A     131          43      9\n",
       "C      15         113      2\n",
       "D      90          11    118"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 选取指定行标签数据\n",
    "df2.loc[['A', 'C', 'D']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "9f7a5a1b-2f7a-4323-aca8-5ffa89ff4513",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Keras</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>131</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>11</td>\n",
       "      <td>140</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>15</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>90</td>\n",
       "      <td>118</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>55</td>\n",
       "      <td>119</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  Keras\n",
       "A     131      9\n",
       "B      11    140\n",
       "C      15      2\n",
       "D      90    118\n",
       "E      55    119"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 根据行标签切片，选取指定列标签的数据\n",
    "df2.loc['A': 'E', ['Python', 'Keras']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "68c7ea93-913c-4081-9d62-107323ee0548",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Keras</th>\n",
       "      <th>Tensorflow</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>9</td>\n",
       "      <td>43</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>140</td>\n",
       "      <td>102</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>2</td>\n",
       "      <td>113</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>118</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>119</td>\n",
       "      <td>77</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>F</th>\n",
       "      <td>115</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>G</th>\n",
       "      <td>87</td>\n",
       "      <td>135</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>H</th>\n",
       "      <td>15</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I</th>\n",
       "      <td>83</td>\n",
       "      <td>36</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>J</th>\n",
       "      <td>105</td>\n",
       "      <td>51</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Keras  Tensorflow\n",
       "A      9          43\n",
       "B    140         102\n",
       "C      2         113\n",
       "D    118          11\n",
       "E    119          77\n",
       "F    115           8\n",
       "G     87         135\n",
       "H     15           1\n",
       "I     83          36\n",
       "J    105          51"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# :默认保留所有行\n",
    "df2.loc[:, ['Keras', 'Tensorflow']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "7cfa7cb9-8e0e-486f-bf54-03178adcdea7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Tensorflow</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>55</td>\n",
       "      <td>77</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>G</th>\n",
       "      <td>100</td>\n",
       "      <td>135</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I</th>\n",
       "      <td>45</td>\n",
       "      <td>36</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  Tensorflow\n",
       "E      55          77\n",
       "G     100         135\n",
       "I      45          36"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 行切片从标签E开始每2个抽取一个，列标签进行切片\n",
    "df2.loc['E'::2, 'Python': 'Tensorflow']   # 如果你填写的是名字，那么左右都是闭区间，这个和索引不太一样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "94779f52-5bde-4e44-95a5-9622546a92e3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "131"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 选取标量值\n",
    "df2.loc['A', 'Python']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c15a235-6afd-49c5-b741-d104ac1c55bd",
   "metadata": {},
   "source": [
    "## （3）位置选择"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "a1d191bb-3cfb-4c00-8611-f2e8436207bd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Tensorflow</th>\n",
       "      <th>Keras</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>115</td>\n",
       "      <td>48</td>\n",
       "      <td>68</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>81</td>\n",
       "      <td>63</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>39</td>\n",
       "      <td>4</td>\n",
       "      <td>79</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>19</td>\n",
       "      <td>82</td>\n",
       "      <td>100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>60</td>\n",
       "      <td>10</td>\n",
       "      <td>144</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>F</th>\n",
       "      <td>92</td>\n",
       "      <td>144</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>G</th>\n",
       "      <td>109</td>\n",
       "      <td>118</td>\n",
       "      <td>75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>H</th>\n",
       "      <td>61</td>\n",
       "      <td>55</td>\n",
       "      <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I</th>\n",
       "      <td>89</td>\n",
       "      <td>121</td>\n",
       "      <td>53</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>J</th>\n",
       "      <td>53</td>\n",
       "      <td>127</td>\n",
       "      <td>141</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  Tensorflow  Keras\n",
       "A     115          48     68\n",
       "B      81          63     74\n",
       "C      39           4     79\n",
       "D      19          82    100\n",
       "E      60          10    144\n",
       "F      92         144     14\n",
       "G     109         118     75\n",
       "H      61          55     27\n",
       "I      89         121     53\n",
       "J      53         127    141"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(0, 150, size=(10, 3))\n",
    "index = list('ABCDEFGHIJ')\n",
    "columns = ['Python','Tensorflow','Keras']\n",
    "\n",
    "df3 = pd.DataFrame(\n",
    "    data=data,\n",
    "    index=index,\n",
    "    columns=columns\n",
    ")\n",
    "\n",
    "df3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "de26662c-4b71-45cd-a715-648f278030e8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Python        83\n",
       "Tensorflow    19\n",
       "Keras          9\n",
       "Name: 4, dtype: int32"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用整数位置选择\n",
    "df.iloc[4]  # 返回第5行数据，Series对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "b21f6dc0-1990-4826-8b28-e6c9de2f2def",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Keras</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>115</td>\n",
       "      <td>68</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>81</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>39</td>\n",
       "      <td>79</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>19</td>\n",
       "      <td>100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>60</td>\n",
       "      <td>144</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  Keras\n",
       "A     115     68\n",
       "B      81     74\n",
       "C      39     79\n",
       "D      19    100\n",
       "E      60    144"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用整数切片\n",
    "df3.iloc[:5, ::2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "86e2490a-17d2-41e1-8510-60aef151eadb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Keras</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>81</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>19</td>\n",
       "      <td>100</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  Keras\n",
       "B      81     74\n",
       "D      19    100"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数组列表按位置切片\n",
    "df3.iloc[[1, 3], [0, 2]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "9dcdf61d-fc1e-489d-831a-cea5367024a4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Tensorflow</th>\n",
       "      <th>Keras</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>81</td>\n",
       "      <td>63</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>39</td>\n",
       "      <td>4</td>\n",
       "      <td>79</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  Tensorflow  Keras\n",
       "B      81          63     74\n",
       "C      39           4     79"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 行切片\n",
    "df3.iloc[1: 3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "651ba00c-1d58-4e7e-bb15-9ca25b110455",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Tensorflow</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>115</td>\n",
       "      <td>48</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>81</td>\n",
       "      <td>63</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>39</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>19</td>\n",
       "      <td>82</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>60</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>F</th>\n",
       "      <td>92</td>\n",
       "      <td>144</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>G</th>\n",
       "      <td>109</td>\n",
       "      <td>118</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>H</th>\n",
       "      <td>61</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I</th>\n",
       "      <td>89</td>\n",
       "      <td>121</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>J</th>\n",
       "      <td>53</td>\n",
       "      <td>127</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  Tensorflow\n",
       "A     115          48\n",
       "B      81          63\n",
       "C      39           4\n",
       "D      19          82\n",
       "E      60          10\n",
       "F      92         144\n",
       "G     109         118\n",
       "H      61          55\n",
       "I      89         121\n",
       "J      53         127"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 列切片\n",
    "df3.iloc[:, :2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "04ceb099-18a9-4dce-be58-ede8d4392b32",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "68"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 选取某一个具体标量值\n",
    "df3.iloc[0, 2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "514be9da-40ac-45b4-95ee-cac6c5b7f788",
   "metadata": {},
   "source": [
    "## （4）布尔索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "b52b8021-79bf-42e3-b5c6-d73628d4c1be",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Tensorflow</th>\n",
       "      <th>Keras</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>131</td>\n",
       "      <td>43</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>11</td>\n",
       "      <td>102</td>\n",
       "      <td>140</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>15</td>\n",
       "      <td>113</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>90</td>\n",
       "      <td>11</td>\n",
       "      <td>118</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>55</td>\n",
       "      <td>77</td>\n",
       "      <td>119</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>F</th>\n",
       "      <td>130</td>\n",
       "      <td>8</td>\n",
       "      <td>115</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>G</th>\n",
       "      <td>100</td>\n",
       "      <td>135</td>\n",
       "      <td>87</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>H</th>\n",
       "      <td>26</td>\n",
       "      <td>1</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I</th>\n",
       "      <td>45</td>\n",
       "      <td>36</td>\n",
       "      <td>83</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>J</th>\n",
       "      <td>94</td>\n",
       "      <td>51</td>\n",
       "      <td>105</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  Tensorflow  Keras\n",
       "A     131          43      9\n",
       "B      11         102    140\n",
       "C      15         113      2\n",
       "D      90          11    118\n",
       "E      55          77    119\n",
       "F     130           8    115\n",
       "G     100         135     87\n",
       "H      26           1     15\n",
       "I      45          36     83\n",
       "J      94          51    105"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 深拷贝一个df\n",
    "df4 = df2.copy()\n",
    "df4\n",
    "# 在Pandas中，view()才是浅拷贝，copy()直接是深拷贝\n",
    "# 在Python中，copy()是浅拷贝，deepcopy()是深拷贝"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "bb0de5af-6388-40bf-a3b5-02c9c86d14be",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Tensorflow</th>\n",
       "      <th>Keras</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>15</td>\n",
       "      <td>48</td>\n",
       "      <td>149</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>11</td>\n",
       "      <td>102</td>\n",
       "      <td>140</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>15</td>\n",
       "      <td>113</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>90</td>\n",
       "      <td>11</td>\n",
       "      <td>118</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>55</td>\n",
       "      <td>77</td>\n",
       "      <td>119</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>F</th>\n",
       "      <td>130</td>\n",
       "      <td>8</td>\n",
       "      <td>115</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>G</th>\n",
       "      <td>100</td>\n",
       "      <td>135</td>\n",
       "      <td>87</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>H</th>\n",
       "      <td>26</td>\n",
       "      <td>1</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I</th>\n",
       "      <td>45</td>\n",
       "      <td>36</td>\n",
       "      <td>83</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>J</th>\n",
       "      <td>94</td>\n",
       "      <td>51</td>\n",
       "      <td>105</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  Tensorflow  Keras\n",
       "A      15          48    149\n",
       "B      11         102    140\n",
       "C      15         113      2\n",
       "D      90          11    118\n",
       "E      55          77    119\n",
       "F     130           8    115\n",
       "G     100         135     87\n",
       "H      26           1     15\n",
       "I      45          36     83\n",
       "J      94          51    105"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df4.iloc[0] = np.random.randint(0, 150, size=(1, 3))\n",
    "df4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "d2b8752e-bd9b-43d9-9256-3cb246a2cf64",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    False\n",
       "B    False\n",
       "C    False\n",
       "D    False\n",
       "E    False\n",
       "F     True\n",
       "G    False\n",
       "H    False\n",
       "I    False\n",
       "J    False\n",
       "Name: Python, dtype: bool"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cond1 = df4.Python > 100\n",
    "cond1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "2e6dde01-d1e4-4223-88ec-0ae652deb077",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Tensorflow</th>\n",
       "      <th>Keras</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>F</th>\n",
       "      <td>130</td>\n",
       "      <td>8</td>\n",
       "      <td>115</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  Tensorflow  Keras\n",
       "F     130           8    115"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df4[cond1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "c1654307-4eab-4ed1-95be-0c452c5693da",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    False\n",
       "B    False\n",
       "C    False\n",
       "D     True\n",
       "E     True\n",
       "F     True\n",
       "G     True\n",
       "H    False\n",
       "I    False\n",
       "J     True\n",
       "dtype: bool"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cond2 = (df4.Python > 50) & (df4['Keras'] > 50)\n",
    "cond2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "390d6659-ff84-479e-8286-201ac5c62ac9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Tensorflow</th>\n",
       "      <th>Keras</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>90</td>\n",
       "      <td>11</td>\n",
       "      <td>118</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>55</td>\n",
       "      <td>77</td>\n",
       "      <td>119</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>F</th>\n",
       "      <td>130</td>\n",
       "      <td>8</td>\n",
       "      <td>115</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>G</th>\n",
       "      <td>100</td>\n",
       "      <td>135</td>\n",
       "      <td>87</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>J</th>\n",
       "      <td>94</td>\n",
       "      <td>51</td>\n",
       "      <td>105</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  Tensorflow  Keras\n",
       "D      90          11    118\n",
       "E      55          77    119\n",
       "F     130           8    115\n",
       "G     100         135     87\n",
       "J      94          51    105"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df4[cond2]   # 返回Python和Keras同时大于50分的 "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59724d82-f9a8-4f39-9808-d0fe19d0c557",
   "metadata": {},
   "source": [
    "## （5）赋值操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "41272f64-5236-4b90-b697-9aa84cb16ac4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Tensorflow</th>\n",
       "      <th>Keras</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>131</td>\n",
       "      <td>43</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>11</td>\n",
       "      <td>102</td>\n",
       "      <td>140</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>15</td>\n",
       "      <td>113</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>90</td>\n",
       "      <td>11</td>\n",
       "      <td>118</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>55</td>\n",
       "      <td>77</td>\n",
       "      <td>119</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>F</th>\n",
       "      <td>130</td>\n",
       "      <td>8</td>\n",
       "      <td>115</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>G</th>\n",
       "      <td>100</td>\n",
       "      <td>135</td>\n",
       "      <td>87</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>H</th>\n",
       "      <td>26</td>\n",
       "      <td>1</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I</th>\n",
       "      <td>45</td>\n",
       "      <td>36</td>\n",
       "      <td>83</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>J</th>\n",
       "      <td>94</td>\n",
       "      <td>51</td>\n",
       "      <td>105</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  Tensorflow  Keras\n",
       "A     131          43      9\n",
       "B      11         102    140\n",
       "C      15         113      2\n",
       "D      90          11    118\n",
       "E      55          77    119\n",
       "F     130           8    115\n",
       "G     100         135     87\n",
       "H      26           1     15\n",
       "I      45          36     83\n",
       "J      94          51    105"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df5 = df2.copy()\n",
    "df5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "631e8b37-ff44-48ae-8bdb-94897b32b094",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A     16\n",
       "B     78\n",
       "C     28\n",
       "D    139\n",
       "E     60\n",
       "F     29\n",
       "G    128\n",
       "H    142\n",
       "I     40\n",
       "J     21\n",
       "dtype: int32"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 单独创建一个Series，添加到df5中\n",
    "s = pd.Series(\n",
    "    data=np.random.randint(0, 150, size=(10,)),\n",
    "    # name='Pytorch',\n",
    "    index=list('ABCDEFGHIJ')\n",
    ")\n",
    "s   # 这个s要注意，index 得和df5匹配才能插入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "0fe99cdc-e595-47d3-a059-21c965bea288",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Tensorflow</th>\n",
       "      <th>Keras</th>\n",
       "      <th>Pytorch</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>131</td>\n",
       "      <td>43</td>\n",
       "      <td>9</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>11</td>\n",
       "      <td>102</td>\n",
       "      <td>140</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>15</td>\n",
       "      <td>113</td>\n",
       "      <td>2</td>\n",
       "      <td>28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>90</td>\n",
       "      <td>11</td>\n",
       "      <td>118</td>\n",
       "      <td>139</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>55</td>\n",
       "      <td>77</td>\n",
       "      <td>119</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>F</th>\n",
       "      <td>130</td>\n",
       "      <td>8</td>\n",
       "      <td>115</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>G</th>\n",
       "      <td>100</td>\n",
       "      <td>135</td>\n",
       "      <td>87</td>\n",
       "      <td>128</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>H</th>\n",
       "      <td>26</td>\n",
       "      <td>1</td>\n",
       "      <td>15</td>\n",
       "      <td>142</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I</th>\n",
       "      <td>45</td>\n",
       "      <td>36</td>\n",
       "      <td>83</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>J</th>\n",
       "      <td>94</td>\n",
       "      <td>51</td>\n",
       "      <td>105</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  Tensorflow  Keras  Pytorch\n",
       "A     131          43      9       16\n",
       "B      11         102    140       78\n",
       "C      15         113      2       28\n",
       "D      90          11    118      139\n",
       "E      55          77    119       60\n",
       "F     130           8    115       29\n",
       "G     100         135     87      128\n",
       "H      26           1     15      142\n",
       "I      45          36     83       40\n",
       "J      94          51    105       21"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将s添加到\n",
    "df5['Pytorch'] = s\n",
    "df5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "be50e1e7-eac2-4d30-97fb-a11f76e65141",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 对标量赋值\n",
    "df5.iloc[3, 2] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "2b7f8306-72c6-4f64-9796-e5e2e53207f3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Tensorflow</th>\n",
       "      <th>Keras</th>\n",
       "      <th>Pytorch</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>131</td>\n",
       "      <td>43</td>\n",
       "      <td>9</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>11</td>\n",
       "      <td>102</td>\n",
       "      <td>140</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>15</td>\n",
       "      <td>113</td>\n",
       "      <td>2</td>\n",
       "      <td>28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>90</td>\n",
       "      <td>11</td>\n",
       "      <td>0</td>\n",
       "      <td>139</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>55</td>\n",
       "      <td>77</td>\n",
       "      <td>119</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>F</th>\n",
       "      <td>130</td>\n",
       "      <td>8</td>\n",
       "      <td>115</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>G</th>\n",
       "      <td>100</td>\n",
       "      <td>135</td>\n",
       "      <td>87</td>\n",
       "      <td>128</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>H</th>\n",
       "      <td>26</td>\n",
       "      <td>1</td>\n",
       "      <td>15</td>\n",
       "      <td>142</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I</th>\n",
       "      <td>45</td>\n",
       "      <td>36</td>\n",
       "      <td>83</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>J</th>\n",
       "      <td>94</td>\n",
       "      <td>51</td>\n",
       "      <td>105</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  Tensorflow  Keras  Pytorch\n",
       "A     131          43      9       16\n",
       "B      11         102    140       78\n",
       "C      15         113      2       28\n",
       "D      90          11      0      139\n",
       "E      55          77    119       60\n",
       "F     130           8    115       29\n",
       "G     100         135     87      128\n",
       "H      26           1     15      142\n",
       "I      45          36     83       40\n",
       "J      94          51    105       21"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "7f22a434-0f20-4cc1-9648-7ffa3f4d04c6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Tensorflow</th>\n",
       "      <th>Keras</th>\n",
       "      <th>Pytorch</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>128</td>\n",
       "      <td>43</td>\n",
       "      <td>9</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>128</td>\n",
       "      <td>102</td>\n",
       "      <td>140</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>128</td>\n",
       "      <td>113</td>\n",
       "      <td>2</td>\n",
       "      <td>28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>128</td>\n",
       "      <td>11</td>\n",
       "      <td>0</td>\n",
       "      <td>139</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>128</td>\n",
       "      <td>77</td>\n",
       "      <td>119</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>F</th>\n",
       "      <td>128</td>\n",
       "      <td>8</td>\n",
       "      <td>115</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>G</th>\n",
       "      <td>128</td>\n",
       "      <td>135</td>\n",
       "      <td>87</td>\n",
       "      <td>128</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>H</th>\n",
       "      <td>128</td>\n",
       "      <td>1</td>\n",
       "      <td>15</td>\n",
       "      <td>142</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I</th>\n",
       "      <td>128</td>\n",
       "      <td>36</td>\n",
       "      <td>83</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>J</th>\n",
       "      <td>128</td>\n",
       "      <td>51</td>\n",
       "      <td>105</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  Tensorflow  Keras  Pytorch\n",
       "A     128          43      9       16\n",
       "B     128         102    140       78\n",
       "C     128         113      2       28\n",
       "D     128          11      0      139\n",
       "E     128          77    119       60\n",
       "F     128           8    115       29\n",
       "G     128         135     87      128\n",
       "H     128           1     15      142\n",
       "I     128          36     83       40\n",
       "J     128          51    105       21"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 按numpy数组进行赋值\n",
    "df5.loc[:, 'Python'] = np.array([128]*10)\n",
    "df5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "a0ddf63a-9860-4ec7-947f-826964ab9de3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 按照条件进行赋值，大于等于128变成负数\n",
    "df5[df5 >= 128] = -df5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "5002e824-0ac3-4e97-b07a-d556c37a1e11",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Tensorflow</th>\n",
       "      <th>Keras</th>\n",
       "      <th>Pytorch</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>-128</td>\n",
       "      <td>43</td>\n",
       "      <td>9</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>-128</td>\n",
       "      <td>102</td>\n",
       "      <td>-140</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>-128</td>\n",
       "      <td>113</td>\n",
       "      <td>2</td>\n",
       "      <td>28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>-128</td>\n",
       "      <td>11</td>\n",
       "      <td>0</td>\n",
       "      <td>-139</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>-128</td>\n",
       "      <td>77</td>\n",
       "      <td>119</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>F</th>\n",
       "      <td>-128</td>\n",
       "      <td>8</td>\n",
       "      <td>115</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>G</th>\n",
       "      <td>-128</td>\n",
       "      <td>-135</td>\n",
       "      <td>87</td>\n",
       "      <td>-128</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>H</th>\n",
       "      <td>-128</td>\n",
       "      <td>1</td>\n",
       "      <td>15</td>\n",
       "      <td>-142</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I</th>\n",
       "      <td>-128</td>\n",
       "      <td>36</td>\n",
       "      <td>83</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>J</th>\n",
       "      <td>-128</td>\n",
       "      <td>51</td>\n",
       "      <td>105</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  Tensorflow  Keras  Pytorch\n",
       "A    -128          43      9       16\n",
       "B    -128         102   -140       78\n",
       "C    -128         113      2       28\n",
       "D    -128          11      0     -139\n",
       "E    -128          77    119       60\n",
       "F    -128           8    115       29\n",
       "G    -128        -135     87     -128\n",
       "H    -128           1     15     -142\n",
       "I    -128          36     83       40\n",
       "J    -128          51    105       21"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "d035b915-b15f-4ab0-9e36-6935a625451a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 在指定位置插入一列\n",
    "df5.insert(\n",
    "    loc=1, # 指定插入位置\n",
    "    column='xxx',   # 插入的列名叫 xxx\n",
    "    value=1024   # 插入的值\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "b069081e-4a55-4cb2-9058-599df738e086",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>xxx</th>\n",
       "      <th>Tensorflow</th>\n",
       "      <th>Keras</th>\n",
       "      <th>Pytorch</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>-128</td>\n",
       "      <td>1024</td>\n",
       "      <td>43</td>\n",
       "      <td>9</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>-128</td>\n",
       "      <td>1024</td>\n",
       "      <td>102</td>\n",
       "      <td>-140</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>-128</td>\n",
       "      <td>1024</td>\n",
       "      <td>113</td>\n",
       "      <td>2</td>\n",
       "      <td>28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>-128</td>\n",
       "      <td>1024</td>\n",
       "      <td>11</td>\n",
       "      <td>0</td>\n",
       "      <td>-139</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>-128</td>\n",
       "      <td>1024</td>\n",
       "      <td>77</td>\n",
       "      <td>119</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>F</th>\n",
       "      <td>-128</td>\n",
       "      <td>1024</td>\n",
       "      <td>8</td>\n",
       "      <td>115</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>G</th>\n",
       "      <td>-128</td>\n",
       "      <td>1024</td>\n",
       "      <td>-135</td>\n",
       "      <td>87</td>\n",
       "      <td>-128</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>H</th>\n",
       "      <td>-128</td>\n",
       "      <td>1024</td>\n",
       "      <td>1</td>\n",
       "      <td>15</td>\n",
       "      <td>-142</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I</th>\n",
       "      <td>-128</td>\n",
       "      <td>1024</td>\n",
       "      <td>36</td>\n",
       "      <td>83</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>J</th>\n",
       "      <td>-128</td>\n",
       "      <td>1024</td>\n",
       "      <td>51</td>\n",
       "      <td>105</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python   xxx  Tensorflow  Keras  Pytorch\n",
       "A    -128  1024          43      9       16\n",
       "B    -128  1024         102   -140       78\n",
       "C    -128  1024         113      2       28\n",
       "D    -128  1024          11      0     -139\n",
       "E    -128  1024          77    119       60\n",
       "F    -128  1024           8    115       29\n",
       "G    -128  1024        -135     87     -128\n",
       "H    -128  1024           1     15     -142\n",
       "I    -128  1024          36     83       40\n",
       "J    -128  1024          51    105       21"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "64e8b92e-f502-459d-97c1-3e010012c2b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "#删除一列\n",
    "del df5['xxx']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "6df86860-f891-4d11-ae44-3b06928efbfd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Tensorflow</th>\n",
       "      <th>Keras</th>\n",
       "      <th>Pytorch</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>-128</td>\n",
       "      <td>43</td>\n",
       "      <td>9</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>-128</td>\n",
       "      <td>102</td>\n",
       "      <td>-140</td>\n",
       "      <td>78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>-128</td>\n",
       "      <td>113</td>\n",
       "      <td>2</td>\n",
       "      <td>28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>-128</td>\n",
       "      <td>11</td>\n",
       "      <td>0</td>\n",
       "      <td>-139</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>-128</td>\n",
       "      <td>77</td>\n",
       "      <td>119</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>F</th>\n",
       "      <td>-128</td>\n",
       "      <td>8</td>\n",
       "      <td>115</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>G</th>\n",
       "      <td>-128</td>\n",
       "      <td>-135</td>\n",
       "      <td>87</td>\n",
       "      <td>-128</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>H</th>\n",
       "      <td>-128</td>\n",
       "      <td>1</td>\n",
       "      <td>15</td>\n",
       "      <td>-142</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I</th>\n",
       "      <td>-128</td>\n",
       "      <td>36</td>\n",
       "      <td>83</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>J</th>\n",
       "      <td>-128</td>\n",
       "      <td>51</td>\n",
       "      <td>105</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  Tensorflow  Keras  Pytorch\n",
       "A    -128          43      9       16\n",
       "B    -128         102   -140       78\n",
       "C    -128         113      2       28\n",
       "D    -128          11      0     -139\n",
       "E    -128          77    119       60\n",
       "F    -128           8    115       29\n",
       "G    -128        -135     87     -128\n",
       "H    -128           1     15     -142\n",
       "I    -128          36     83       40\n",
       "J    -128          51    105       21"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4025fbed-1ec5-4181-a551-96af7fbf3d06",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
